<?php

// +----------------------------------------------------------------------
// | Easyadmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller\apply;
use app\common\controller\AdminController;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

use app\admin\model\website\LuckCate;
use app\admin\model\website\LuckList as LuckListModel;


/**
 * Class Admin
 * @package app\admin\controller\user
 * @ControllerAnnotation(title="站点管理")
 */
class LuckList extends AdminController
{

    use \app\admin\traits\Curd;

    protected $sort = [
        'id'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new LuckListModel();
    }

    /**
     * 奖品列表
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            

            foreach ($where as &$item) {
                if ($item[0] == 'name') {
                    $item[0]= 'c.name';
                }else {
                    $item[0]= 'l.'.$item[0];
                }
            }

            $count = $this->model
            ->alias("l")
            ->join('luck_cate c','c.id = l.cid')
            ->where($where)
            ->count();


            $list = $this->model
                ->alias("l")
                ->join('luck_cate c','c.id = l.cid')
                ->where($where)
                ->field("c.name,c.images,l.*")
                ->page($page, $limit)
                ->order($this->sort)
                ->select()->each(function ($item) {
                    $item['name'] = $item['name'] .' '. $item['number'];
                });
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $cateList = LuckCate::order("id desc")->select()->toarray();
        $this->assign("cate",$cateList);
        return $this->fetch();
    }

    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        $cateList = LuckCate::order("id desc")->select()->toarray();
        $this->assign("cate",$cateList);
        return $this->fetch();
    }

    
}
